package com.sls.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import com.sls.web.manager.game.parser.ParseXmlDocument;
import com.sls.web.manager.game.pojo.xqt.XQTDiscountInfo;

public class FileUtil {

	public static void main(String[] args) throws FileNotFoundException,
			IOException {
		String destFolder = "F:/xmldoc_new";
		//上游接口文档及产品信息\\星启天\\接口开发文档(最新)\\外部接口\\game\\xmldoc\\
		File xml = new File(
				"F:\\上游接口文档及产品信息\\星启天\\接口开发文档(最新)\\xmldoc\\gameid.xml");
		List<XQTDiscountInfo> disList = ParseXmlDocument.parseGameIdXml(xml);
		List<File> fileList = readFloder("F:\\上游接口文档及产品信息\\星启天\\接口开发文档(最新)\\xmldoc");
		System.out.println(disList);
		for (XQTDiscountInfo disInfo : disList) {
			String recordId = disInfo.getRecordId();
			for (File file : fileList) {
				String filename = file.getName();
				String[] fileNameParts = filename.split("\\.");
				if (filename.endsWith(".xml")
						&& recordId.equals(fileNameParts[0])) {
					remove(destFolder, file, filename);
				}
			}
		}

	}

	public static void remove(String destFolder, File xml, String xmlFileName)
			throws FileNotFoundException, IOException {
		InputStream is = new FileInputStream(xml);
		File deskFile = new File(destFolder, xmlFileName);
		OutputStream os = new FileOutputStream(deskFile);
		byte[] bytefer = new byte[1024];
		int length = 0;
		while ((length = is.read(bytefer)) > 0) {
			os.write(bytefer, 0, length);
		}
		os.close();
		is.close();
	}

	public static List<File> readFloder(String filepath)
			throws FileNotFoundException, IOException {
		List<File> fileList = new ArrayList<File>();
		try {

			File file = new File(filepath);
			if (!file.isDirectory()) {
				fileList.add(file);

			} else if (file.isDirectory()) {
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					File readfile = new File(filepath + File.separator
							+ filelist[i]);
					if (!readfile.isDirectory()
							&& readfile.getName().endsWith(".xml")) {
						fileList.add(readfile);
					} else if (readfile.isDirectory()) {
						fileList = readFloder(filepath + File.separator
								+ filelist[i]);
					}
				}
			}
		} catch (FileNotFoundException e) {
			System.out.println("readfile()   Exception:" + e.getMessage());
		}
		return fileList;
	}

}
